package PLAYER;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.border.Border;


public class PlayerWindow extends JFrame implements ActionListener
{
	String nick;
	int win_width=800;
	int win_high=700;
	int players_number;
	int my_player_id;
	int[][] players_locations_table = new int [6][2];
	ArrayList <ImageIcon> cards_images= new ArrayList<ImageIcon>();
	ArrayList <PlayerOther> players_list = new ArrayList<PlayerOther>();
	
	JPanel main, buttons, about, players_icon[];
	JLabel chips[], nickfield[], status[];
	JButton card[];
	
	PlayerWindow(int my_player_id, int players_number, String nick)
	{

		this.nick = nick;
		this.setTitle(this.nick);
		this.getContentPane().setBackground(new Color(61,61,61));
		this.setSize(win_width,win_high);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setLayout(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.players_number=players_number;
		this.my_player_id=my_player_id;
	
		main = new JPanel();
		main.setLayout(null);
		main.setBounds(10,10,win_width-24,win_high-160);
		main.setBorder(BorderFactory.createEmptyBorder());
		main.setBackground(new Color(121,121,121));
		this.add(main);
		
		buttons = new JPanel();
		buttons.setBounds(10,win_high-140,win_width-24,105);
		buttons.setBorder(BorderFactory.createEmptyBorder());
		buttons.setBackground(new Color(121,121,121));
		this.add(buttons);


		createPlayerList();
		setPlayersLocationTable();
		loadCardImages();
		setPlayersView();
		setPlayersStatusIcon();
		setCardView();

		//update();
		//changeCardView();
	}

	public void actionPerformed(ActionEvent ae)
	{

	}

	public void setCardView()
	{
		int x = 1;
		int y = 1;
		card = new JButton[4];
		for(int i=0; i<=3; i++)
		{
			card[i] = new JButton(cards_images.get(0));
			card[i].setSize(70,93);
			card[i].setLocation(x, y);
			card[i].addActionListener(this);
			players_icon[my_player_id].add(card[i]);
			x+=71;
		}
	}
	
	public void changeCardView(String [] new_cards)
	{
		for(int i=0; i<4; i++)
		{
			card[i].setIcon(cards_images.get(Integer.parseInt(new_cards[i+1])));
		}
	}

	public void changeChipsForAll(String [] new_chips)
	{
		int j=0;
		for(int i=0; i<players_list.size();i++)
		{
			j++;
			players_list.get(i).chips = Integer.parseInt(new_chips[j*2]);
			if (i==my_player_id)
			{
				//zmiana jeszcze bedzie;
				continue;
			}
			else
			{
				int update_position = players_list.get(i).position_number;
				chips[update_position].setText(new_chips[j * 2]);
			}

		}
	}

	public void createPlayerList()
	{
		for(int i=0; i<players_number; i++)
		{
			players_list.add(new PlayerOther(i, 0, " "));
			players_list.get(i).position_number=i;
		}
	}

	public void setPlayersNick(String[] nick)
	{
		for(int i=0; i<players_list.size(); i++)
		{
			players_list.get(i).nickname = nick[i+1];
			if (i==my_player_id)
			{
				continue;
			}
			else
			{
				int update_position = players_list.get(i).position_number;
				nickfield[update_position].setText(nick[i + 1]);
			}
		}
	}

	public void deletePlayer(String []which)
	{
		int index = Integer.parseInt(which[1]);
		if (index<my_player_id);
		{
			my_player_id--;
		}

		int position_do_hide = players_list.get(index).position_number;
		players_icon[position_do_hide].setVisible(false);
		players_list.remove(index);

	}

	public void setPlayersLocationTable()
	{
		//on the bottom right
		players_locations_table[0][0]=(main.getWidth()/3)*2-120;
		players_locations_table[0][1]=main.getHeight()-110;

		//on the bottom left
		players_locations_table[1][0]=main.getWidth()/3-80;
		players_locations_table[1][1]=main.getHeight()-110;

		//on the left
		players_locations_table[2][0]=10;
		players_locations_table[2][1]=main.getHeight()/2-46;

		//on the up left
		players_locations_table[3][0]=main.getWidth()/3-60;
		players_locations_table[3][1]=10;

		//on the up right
		players_locations_table[4][0]=(main.getWidth()/3)*2-35;
		players_locations_table[4][1]=10;

		//on the right
		players_locations_table[5][0]=main.getWidth()-90;
		players_locations_table[5][1]=main.getHeight()/2-46;
	}
	public void setPlayersView()
	{
		//int x=10;
		//int y=10;

		players_icon = new JPanel[players_number];
		int j=1;


		for(int i=my_player_id+1; i<players_number; i++)
		{
			players_icon[i] = new JPanel();
			players_icon[i].setSize(80,93);
			players_icon[i].setLayout(null);
			players_icon[i].setLocation(players_locations_table[j][0], players_locations_table[j][1]);
			main.add(players_icon[i]);
			j++;
		}
		j=players_locations_table.length-1;
		for (int i=my_player_id-1; i>=0; i--)
		{
			players_icon[i] = new JPanel();
			players_icon[i].setSize(80,93);
			players_icon[i].setLayout(null);
			players_icon[i].setLocation(players_locations_table[j][0], players_locations_table[j][1]);
			main.add(players_icon[i]);
			j--;
		}

		players_icon[my_player_id]=new JPanel();
		players_icon[my_player_id].setSize(284,95);
		players_icon[my_player_id].setLocation(players_locations_table[0][0], players_locations_table[0][1]);
		players_icon[my_player_id].setLayout(null);
		main.add(players_icon[my_player_id]);



	}

	public void setPlayersStatusIcon()
	{
		chips = new JLabel[players_number];
		nickfield = new JLabel[players_number];
		for (int i=0; i<players_number; i++)
		{
			if (i == my_player_id)
			{
				continue;
			}
			else
			{
				chips[i] = new JLabel();
				chips[i].setBounds(5,20,20,10);
				nickfield[i] = new JLabel();
				nickfield[i].setBounds(5,5,20,10);
				players_icon[i].add(chips[i]);
				players_icon[i].add(nickfield[i]);
			}
		}
	}
	public void loadCardImages()
	{
		char [] symbol = {'c','d','h','s'};
		int i=1;
		int k=0;
		cards_images.add(0, new ImageIcon(getClass().getResource("..//cards//b1.jpg")));
		int index=1;
		while(i<14)
		{
			cards_images.add(index, new ImageIcon(getClass().getResource("..//cards//"+String.valueOf(symbol[k])+String.valueOf(i)+".jpg")));
			System.out.println("..//cards//" + String.valueOf(symbol[k]) + String.valueOf(i) + ".jpg " + i+" "+index);
			if(i==13&&k<3)
			{
				i=1;
				k++;
				index++;
				continue;
			}
			else if (i==13&&k==3)
			{
				break;
			}
			index++;
			i++;
		}
		//System.out.println(cards_images.size());
	}


	public static String [] decoder(String about)
	{

		String results [];

			results = about.split(" ");

		return results;
	}
}
